package com.xingkeduo.business.user.service;

import com.xingkeduo.dao.database.DatabaseAccessObject;
import com.xingkeduo.pojo.query.CraftsmanSocialHistoryQO;
import com.xingkeduo.pojo.query.CraftsmanSocialQO;
import com.xingkeduo.pojo.table.CraftsmanSocialHistoryTO;
import com.xingkeduo.pojo.table.CraftsmanSocialTO;
import com.xingkeduo.util.pojo.enums.HistoryTypeEnum;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.tool.CommonUtil;
import com.xingkeduo.util.tool.IdentityUtil;
import com.xingkeduo.util.tool.IdentityUtil.Worker;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 一线员工工作属性类
 *
 * @author 高杰
 * @time 2018年12月27日 上午11:45:44
 * @description
 */
@Component
public class CraftsmanSocialService {
    private static final String NAMESPACE = CommonUtil.formatNamespace(CraftsmanSocialTO.class);
    private static final String NAMESPACE_HISTORY = CommonUtil.formatNamespace(CraftsmanSocialHistoryTO.class);
    @Resource(name = "mysqlUserMasterDao")
    private DatabaseAccessObject<CraftsmanSocialTO, CraftsmanSocialQO> dao;
    @Resource(name = "mysqlUserMasterDao")
    private DatabaseAccessObject<CraftsmanSocialHistoryTO, CraftsmanSocialHistoryQO> historyDao;

    /**
     * 查询
     *
     * @param code
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2018年12月27日 上午11:57:50
     */
    public CraftsmanSocialTO selectByPrimary(long id) throws Throwable {
        return dao.select(NAMESPACE, id);
    }

    /**
     * 查询主键
     *
     * @param query
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2019年1月8日 下午2:18:51
     */
    public List<Long> selectPrimaryByQuery(CraftsmanSocialQO query) throws Throwable {
        return dao.selectIndex(NAMESPACE, query);
    }

    /**
     * 查询发型师集合
     *
     * @param query:
     * @return : java.util.List<com.xingkeduo.pojo.table.CraftsmanSocialTO>
     * @author : gaozifeng
     * @date : 2019/3/30 14:14
     */
    public List<CraftsmanSocialTO> selectByQuery(CraftsmanSocialQO query) throws Throwable {
        return dao.select(NAMESPACE, query);
    }

    /**
     * 新增查询手机号
     *
     * @param phone
     * @return
     * @throws Throwable
     * @author 高子峰
     * @time 2019年3月19日10:14:18
     */
    public List<Long> selectPrimaryByPhone(String phone) throws Throwable {
        CraftsmanSocialQO qo = new CraftsmanSocialQO().setPhone(phone);
        return dao.selectIndex(NAMESPACE, qo);
    }

    /**
     * @param name
     * @return
     * @throws Throwable
     * @author 高子峰
     * @time 2019年3月19日10:14:18
     */
    public List<Long> selectByName(String name) throws Throwable {
        CraftsmanSocialQO qo = new CraftsmanSocialQO().setName(name);
        return dao.selectIndex(NAMESPACE, qo);
    }

    /**
     * 添加
     *
     * @param craftsmanSocial
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2018年12月27日 上午11:57:56
     */
    @Transactional
    public void insert(CraftsmanSocialTO craftsmanSocial) throws Throwable {
        if (dao.insert(NAMESPACE, craftsmanSocial) < 1)
            throw new DataException(DataException.Type.FAIL, "一线员工个人信息");

        CraftsmanSocialHistoryTO history = new CraftsmanSocialHistoryTO()
                .setHistoryId(IdentityUtil.nextId(Worker.USER)).setHistoryType(HistoryTypeEnum.NEW)
                .setCraftsmanSocial(craftsmanSocial.setIsDelete(YesNoEnum.NO).setVersion(1))
                .setCreatedBy(craftsmanSocial.getCreatedBy()).setUpdatedBy(craftsmanSocial.getUpdatedBy());
        historyDao.insert(NAMESPACE_HISTORY, history);
    }

    /**
     * 修改
     *
     * @param craftsmanSocial
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2018年12月27日 上午11:58:02
     */
    @Transactional
    public void update(CraftsmanSocialTO craftsmanSocial) throws Throwable {
        if (dao.update(NAMESPACE, craftsmanSocial) < 1)
            throw new DataException(DataException.Type.FAIL, "一线员工个人信息");

        craftsmanSocial = dao.select(NAMESPACE, craftsmanSocial.getId());

        CraftsmanSocialHistoryTO history = new CraftsmanSocialHistoryTO()
                .setHistoryId(IdentityUtil.nextId(Worker.USER)).setHistoryType(HistoryTypeEnum.NEW)
                .setCraftsmanSocial(craftsmanSocial).setCreatedBy(craftsmanSocial.getCreatedBy())
                .setUpdatedBy(craftsmanSocial.getUpdatedBy());
        historyDao.insert(NAMESPACE_HISTORY, history);
    }
}
